package com.atguigui.leetcode1;

import java.util.Arrays;

/**
 * 2517.礼盒的最大甜蜜度
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/6/1 8:43
 */
public class P2517MaximumTastinessOfCandyBasket {

    public static void main(String[] args) {
        Solution solution = new P2517MaximumTastinessOfCandyBasket().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int maximumTastiness(int[] price, int k) {
            Arrays.sort(price);
            int left = 0, right = price[price.length - 1] - price[0];
            while (left < right) {
                int mid = (left + right + 1) / 2;
                if (check(price, k, mid)) {
                    left = mid;
                } else {
                    right = mid - 1;
                }
            }

            return left;
        }

        private boolean check(int[] price, int k, int tastiness) {
            int prev = Integer.MIN_VALUE / 2;
            int cnt = 0;
            for (int p : price) {
                if (p - prev >= tastiness) {
                    cnt++;
                    prev = p;
                }
            }
            return cnt >= k;
        }
    }
}
